<?php 

	$name = $_POST['name'];
	$cuisines = $_POST['cuisines'];
	$maxPrice = $_POST['maxPrice'];
	$minPrice = $_POST['minPrice'];
	$rankIndex = $_POST['rankIndex'];
	
	$con = mysqli_connect('localhost','root','','cs2102');
	if(mysqli_connect_errno($con))
	{
		echo "error";
	}
	else
	{
		$flag = false;
		if(strlen($name)==0 &&strlen($cuisines) == 0 && strlen($maxPrice) == 0 &&strlen($minPrice) ==0)
			$query = "SELECT * FROM restaurant";
		else{
			$query = "SELECT * FROM restaurant where ";
			if(strlen($name)!=0){
				$query = $query."name = \"$name\" ";
				$flag = true;
			}
			
			if(strlen($cuisines)!=0){
				
				if($flag)
					$query = $query."and cuisines = \"$cuisines\" ";
				else
					$query = $query." cuisines = \"$cuisines\" ";
				$flag = true;
			}
			
			if(strlen($maxPrice)!=0){
				
				if($flag)
					$query = $query."and argC <= $maxPrice ";
				else
					$query = $query."argC <= $maxPrice ";
				$flag = true;
			}

			if(strlen($minPrice)!=0){
				
				if($flag)
					$query = $query."and argC >= $minPrice ";
				else
					$query = $query."argC >= $minPrice ";
				$flag = true;
			}
		}

		if($rankIndex == 0){
			$query = $query." order by name";
		}else if($rankIndex == 1){
			$query = $query." order by argC";
		}else{
			$query = $query." order by cuisines";
		}

		$index =0;
		$result =  mysqli_query($con, $query);
		
		$content = array();
		while($row = mysqli_fetch_array($result)){
			$key = "res" . $index;
			$content[$key] = $row;
			$index++;
		}

		echo json_encode($content);
	}
	
	mysqli_close ($con);
?>
